#include <iostream>
using namespace std;
#define  MAX 50
#define INF 99999999

int n, s;
int str[MAX][MAX];
int longth[MAX];
bool visited[MAX];


int main()
{
    cin >> n >> s;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin>>str[i][j];
        }
        visited[i] = false;
    }

    for (int i = 0; i < n; i++)
    {
        longth[i] = str[s][i];
        if (longth[i] == 0 &&  i != s)
        {
            longth[i] = INF;
        }
    }
    visited[s] = true;

    int min;
    int v=s;
    for (int i = 0; i < n; i++)
    {
        min = INF;
        //寻找最小的那个值
        for (int j = 0; j < n; j++)
        {
            if (min > longth[j] && visited[j] == false)
            {
                min = longth[j];
                v = j;
            }
        }
        visited[v] = true;

        for (int j = 0; j < n; j++)
        {
            if (visited[j] == false && longth[j] > str[v][j] + longth[v] && str[v][j] != 0)
            {
                longth[j] = str[v][j] + longth[v];
            }
        }

    }

    for (int i = 0; i < n; i++)
    {
        if (i != s)
        {

            if (longth[i] == INF)
            {
                cout << -1 << " ";
            }
            else
            {
                cout << longth[i] << " ";
            }
        }
    }
    cout << endl;





    return 0;
}